<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of get_warehouse_difference
 *
 * @author Administrator
 */
class get_warehouse_difference extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('wms/api/sap_model');
        $this->load->model('wms/warehouse_list_model');
        $this->load->model('wms/product_inventory_model');
        $this->load->service('wms/receiving_order_service');
        $this->load->service('wms/inventory_track_service');
    }

    function please_enter_your_password($password)
    {

        if ('pandawill' != $password)
        {
            exit;
        }
        $whs_code_array = array(
            'W005-T03',
            'W005-VNE',
        );

        foreach ($whs_code_array as $whs_code)
        {
            //SAP 库存
            echo '<pre>';
            $sap_whs_list = array();
            $url_base = $this->sap_model->get_sap_url();
            $url = $url_base . '/WebUnit/GetData?UserSign=1&QKey=QKey_Item02&Params=' . ",$whs_code";
            $sap_list = $this->sap_model->callAPIQuick($url);
            foreach ($sap_list as $value)
            {
                if ($value['当前库存'] < 1)
                {
                    continue;
                }
                $sap_whs_list[strtoupper($value['SKU']) . '_' . $whs_code] = $value['当前库存'];
            }
//            var_dump($sap_whs_list);
            //WMS 库存
            $wms_whs_list = array();
            $whs_code_list = $this->warehouse_list_model->select_warehouse($where = array('whs_code_s' => $whs_code));
            $whs_code_array = array_column($whs_code_list, 'whs_code');
            $whs_list = $this->product_inventory_model->get_inventory_list($whs_code_array, $sku = '', $limit = 200000);
            foreach ($whs_list as $value)
            {
                if ($value['on_hand'] < 1)
                {
                    continue;
                }
                $wms_whs_list[strtoupper($value['sku']) . '_' . $whs_code] = $value['on_hand'];
            }
//            var_dump($wms_whs_list);
            //SAP WMS 在途
            $zt_not_in = array();
            $url_zt = $url_base . '/WebUnit/GetData?UserSign=1&QKey=KCZT_QKey_01&Params=' . "$whs_code";
            $zt_list = $this->sap_model->callAPIQuick($url_zt);
            $zt_whs_list = array();
            foreach ($zt_list as $value)
            {
                $zt_whs_list[$value['转储单号']][strtoupper($value['物料号']) . '_' . $whs_code] = $value['数量'];
            }
            foreach ($zt_whs_list as $key => $value)
            {
                $zc_order = 'ZC' . $key;
                $wms_zc = $this->receiving_order_service->get_receiving_order_list($whs = '', $sku = '', $refNum = $zc_order);
                //如果已经录入WMS
                if ($wms_zc)
                {

                    foreach ($wms_zc as $val)
                    {
                        if ($value[$val['sku'] . '_' . $val['whs_code']] && $val['status'] == '1')
                        {
                            if ($zt_not_in[$val['sku'] . '_' . $val['whs_code']])
                            {
                                $zt_not_in[$val['sku'] . '_' . $val['whs_code']]+= $value[$val['sku'] . '_' . $val['whs_code']];
                            }
                            else
                            {
                                $zt_not_in[$val['sku'] . '_' . $val['whs_code']] = $value[$val['sku'] . '_' . $val['whs_code']];
                            }
                        }
                    }
                }
                else
                {
                    //没有录入WMS
                    continue;
                }
            }
            //未入库合并SAP库存
            foreach ($zt_not_in as $key => $value)
            {
                $key=strtoupper($key);
                if ($sap_whs_list[$key])
                {
                    $sap_whs_list[$key]+=$value;
                }
                else
                {
                    $sap_whs_list[$key] = $value;
                }
            }
            //WMS 未上传到SAP数据
            $not_sync_list = array();
            $sync_list = $this->inventory_track_service->get_list($limit = 0, $pagesize = 100000, $orderstr = '', $where = array('sync_status' => 1, 'whs_code' => $whs_code), $wherein = array('type' => array('Y15', 'Y19', 'X4', 'X6', 'Y11', 'Y18', 'Y14', 'Y16', 'Y5', 'Y13')));

            foreach ($sync_list as $value)
            {
                if ($not_sync_list[strtoupper($value['sku']) . '_' . $value['whs_code']])
                {
                    $not_sync_list[strtoupper($value['sku']) . '_' . $value['whs_code']]+=$value['quantity'];
                }
                else
                {
                    $not_sync_list[strtoupper($value['sku']) . '_' . $value['whs_code']] = $value['quantity'];
                }
            }
            //未同步合并WMS库存
            foreach ($not_sync_list as $key => $value)
            {
                if ($wms_whs_list[$key])
                {
                    $wms_whs_list[$key]+=$value;
                }
                else
                {
                    $wms_whs_list[$key] = $value;
                }
            }
            //SAP 和WMS 单个仓库对比 
            $dif_array = array();
            $sap_only = array();
            $wms_only = array();
//            var_dump($sap_whs_list);
//            var_dump($wms_whs_list);
            foreach ($sap_whs_list as $key => $value)
            {
                if ($wms_whs_list[$key] && intval($value) == intval($wms_whs_list[$key]))
                {
                    unset($wms_whs_list[$key]);
                    unset($sap_whs_list[$key]);

                }
                elseif ($wms_whs_list[$key] && intval($value) != intval($wms_whs_list[$key]))
                {
                    $dif_array[]=array(
                        $key,$sap_whs_list[$key],$wms_whs_list[$key]
                    );
                    unset($wms_whs_list[$key]);
                    unset($sap_whs_list[$key]);
                }elseif(!$wms_whs_list[$key]){
                    $sap_only[$key]=$value;
                    unset($sap_whs_list[$key]);
                }
            }
            $wms_only=$wms_whs_list;
            echo $whs_code.'####<br><pre>';
            echo 'get_warehouse_difference:<br>';
            var_dump($dif_array);
            echo 'sap_only:<br>';
            var_dump($sap_only);
            echo 'wms_only:<br>';
            var_dump($wms_only);
        }
    }

}

?>
